﻿#include "stdafx.h"
#include "StateIdleMe.h"
#include "PlayerMe.h"
#include "Terrain.h"
#include "MuNetConnect.h"

StateIdleMe::StateIdleMe(FSM *fsm, PlayerMe *obj) : FSMState(fsm)
{
	m_object = obj;
	m_stateName = "idle";
	m_stateType = FSM::st_idle;
	m_timeCount = 0.0;
}

StateIdleMe::~StateIdleMe()
{
}

bool StateIdleMe::Enter()
{
	m_object->m_animate.SetAnimation(m_object->GetAnimIndex_idle(), 0);
	m_object->m_animPosOffset = 0.0f;
	return true;
}

void StateIdleMe::Run()
{
	switch (m_fsm->GetNextState())
	{
	case FSM::st_attack:
		m_fsm->ChangeToNextState();
		break;
	case FSM::st_dir:
		m_object->SetDir(m_object->m_dirTo);
		m_fsm->SetNextState(FSM::st_idle);
		break;
	case FSM::st_move:
		m_fsm->ChangeToNextState();
		break;
	default:
		m_timeCount += FrameTime::GetInstance()->GetDeltaTime();
		if (m_timeCount > 1.5)
		{
			m_timeCount = 0;
			POINT pt1, pt2;
			pt1 = m_object->GetMapPos();
			auto terr = Terrain::GetInstance();
			pt2.x = terr->m_mousePoint_gl.x / 100;
			pt2.y = terr->m_mousePoint_gl.y / 100;
			if (pt1.x != pt2.x || pt1.y != pt2.y)
			{
				int dir = m_object->CalcDir(pt1, pt2);
				if (dir != m_oldDir)
				{
					m_oldDir = dir;
					m_object->SetDir(dir);
					MuNetConnect::GetInstance()->SendMapDir(m_object->m_mapDir);
				}
			}
			
		}
		break;
	}


}
